package com.itheima.fillter;


import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import reactor.core.publisher.Mono;

/**
 * 创建三个过滤器 演示过滤器的执行顺序
 */
@Configuration
@Slf4j
public class FilterConfiguration {

    @Bean
    @Order(-1)
    public GlobalFilter filter1(){
        return ((exchange, chain) -> {
            //前置处理
            log.info("过滤器1的pre处理阶段");
           return chain.filter(exchange).then(
                   Mono.fromRunnable(()->{
                       log.info("过滤器1的post阶段");
                   })
           );//.then方法中  实现过滤器的后置处理
        });
    }

    @Bean
    @Order(0)
    public GlobalFilter filter2(){
        return ((exchange, chain) -> {
            //前置处理
            log.info("过滤器2的pre处理阶段");
            return chain.filter(exchange).then(
                    Mono.fromRunnable(()->{
                        log.info("过滤器2的post阶段");
                    })
            );//.then方法中  实现过滤器的后置处理
        });
    }

    @Bean
    @Order(1)
    public GlobalFilter filter3(){
        return ((exchange, chain) -> {
            //前置处理
            log.info("过滤器3的pre处理阶段");
            return chain.filter(exchange).then(
                    Mono.fromRunnable(()->{
                        log.info("过滤器3的post阶段");
                    })
            );//.then方法中  实现过滤器的后置处理
        });
    }


}
